<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-input v-model="query.name" placeholder="姓名" class="handle-input mr10"></el-input>
        <el-select placeholder="对应渠道" v-model="query.roleId" class="handle-input mr10">
          <el-option v-for="option in option.role" :key='option.id' :value="option.id" :label="option.name" />
        </el-select>
        <el-button type="primary" icon="search" @click="handleSearch">
          <i class="el-icon-search"> 搜索</i>
        </el-button>
        <el-button type="primary" @click="handleRefresh">
          <i class="el-icon-refresh"> 重置</i>
        </el-button>
        <el-button style="float: right;" icon="el-icon-lx-add" type="button" v-has="'sysuser_add'" @click="() => handleAdd()">新增</el-button>
      </div>
      <el-table :data="list.data" border v-loading="list.loading" class="table" ref="multipleTable">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="userName" label="姓名" v-if="this.$permissions.checkColumn('sysuser_userName')">
        </el-table-column>
        <el-table-column prop="loginName" label="登录名" v-if="this.$permissions.checkColumn('sysuser_loginName')">
        </el-table-column>
        <el-table-column prop="roleName" label="角色" v-if="this.$permissions.checkColumn('sysuser_roleName')">
        </el-table-column>
        <el-table-column prop="mark" label="状态" v-if="this.$permissions.checkColumn('sysuser_mark')">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.mark==1" type="success">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240" align="center" v-if="this.$permissions.checkColumn('sysuser_opera')">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-has="'sysuser_edit'">编辑</el-button>
            <el-button type="text" icon="el-icon-refresh" @click="handleResetPwd(scope.row)" v-has="'sysuser_resetPwd'">重置密码</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)" v-has="'sysuser_del'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background @size-change="handlePageSize" @current-change="handlePageIndex" :current-page="query.pageIndex" :page-sizes="[10, 20, 30, 50, 150, 200]" layout="total, sizes, prev, pager, next, jumper" :page-size="query.pageSize" :total="list.total">
        </el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog :title="form.type" :visible.sync="form.show" width="30%">
      <el-form ref="form" :model="form.model" :rules="form.rules" label-width="100px">
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.model.userName"></el-input>
        </el-form-item>
        <el-form-item label="登录名"  prop="loginName">
          <el-input v-model="form.model.loginName"></el-input>
        </el-form-item>
        <el-form-item label="登录密码"  prop="loginPwd" v-if="form.type == '新增'">
          <el-input type="password" v-model="form.model.loginPwd"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select placeholder="角色" v-model="form.model.roleId" class="handle-input mr10">
            <el-option v-for="option in option.role" :key='option.id' :value="option.id" :label="option.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.model.mark">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="form.show = false">取 消</el-button>
        <el-button type="primary" @click="handleSaveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
    import { roleOption } from '@/api/option'
    import { isSelected } from '@/utils/validator'
    import { getPage,save,remove } from '@/api/system/message'
    import md5 from 'js-md5'
    export default {
        data() {
            return {
                query: {
                    title: '',
                    type: 0,
                    pageIndex: 1,
                    pageSize: 10
                },
                list: {
                    loading: false,
                    total: 0,
                    data: []
                },
                option:{
                  role:[]
                },
                form:{
                  show:false,
                  type:"新增",
                  model:{
                    userId:0,
                    loginName:"",
                    loginPwd:"",
                    userName:"",
                    roleId:0,
                    headImg:"",
                    mark:1,
                    remark:""
                  },
                  rules: {
                      userName: [
                          { required: true, message: '请输入姓名', trigger: 'blur' }
                      ],
                      loginName: [
                          { required: true, message: '请输入登录名', trigger: 'blur' }
                      ],
                      loginPwd: [
                          { required: true, message: '请输入登录密码', trigger: 'blur' }
                      ],
                      roleId: [
                          { validator: isSelected, trigger: 'blur' }
                      ]
                  }
                }
            }
        },
        created() {
            this.getListData();
            this.getRoleOption();
            
        },
        computed: {
            data() {
            }
        },
        methods: {
            getListData() {
              this.list.loading = true;
              getPage(this.query).then(res => {
                this.list.data = res.data.items;
                this.list.total = res.data.totalCount;
                this.list.loading = false;
              })
            },
            getRoleOption(){
              roleOption().then(res => {
                this.option.role = res.data
              })
            },
            handlePageSize(pageSize) {
                this.query.pageSize = pageSize
                this.getListData()
            },
            handlePageIndex(pageIndex) {
                this.query.pageIndex = pageIndex
                this.getListData()
            },
            handleSearch() {
                this.query.pageIndex = 1
                this.query.pageSize = 10
                this.getListData()
            },
            handleRefresh() {
              this.query = {
                name: '',
                roleId: 0,
                pageIndex: 1,
                pageSize: 10
              }
              this.getListData()
            },
            handleAdd(){
              this.form.model = {
                    userId:0,
                    loginName:"",
                    loginPwd:"",
                    userName:"",
                    roleId:0,
                    headImg:"",
                    mark:1,
                    remark:""
              }
              this.form.type = "新增";
              this.form.show = true;
            },
            handleEdit(row){
              this.form.model = Object.assign({}, row);
              this.form.type = "编辑";
              this.form.show = true;
            },
            handleSaveEdit(){
              this.$refs["form"].validate((valid) => {
                if (valid) {
                  if(this.form.type === '新增') {
                    this.form.model.loginPwd = md5(this.form.model.loginPwd)
                  }
                  save(this.form.model).then(res => {
                    if (res.status === 200) {
                      this.form.show = false;
                      this.$message({
                        message: "操作成功",
                        type: "success"
                      });
                      this.getListData();
                    }
                  })
                  }
                  else{
                    this.$message({ message: "输入错误", type: 'error' })
                    return false;
                  }
                })
            },
            handleResetPwd(row){
              this.$prompt('请输入密码', '重置密码', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputType:'password',
              }).then(({ value }) => {
                resetPwd({ 
                    UserId: row.userId,
                    Pwd: md5(value)
                  }).then(res => {
                  if (res.status === 200) {
                    this.form.show = false;
                    this.$message({
                      message: "重置成功",
                      type: "success"
                    });
                  }
              }).catch(() => {    
              });
            })
          },
          handleDelete(data) {
            this.$confirm("此操作将永久删除, 是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            })
              .then(() => {
                remove({ Id: data.userId }).then(res => {
                  if (res.status === 200) {
                    this.form.show = false;
                    this.$message({
                      message: "删除成功",
                      type: "success"
                    });
                    this.getListData();
                  }
                });
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消删除"
                });
              });
          },
        }
    }

</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
</style>
